﻿/*
* ProductionManage.js
* zhouh@ZD_20160414
*/
$(function () {
    //announcement
    var filePath = '../UploadFile/'
    var divMsg = new jSharp.popUpDiv('divMsg');
    var divUploading = new jSharp.popUpDiv('divUploading');
    var divEdit = new jSharp.popUpDiv('divEdit');
    var btnAdd = $('#btnAdd');
    //function
    function tips(s) { //消息提示
        divMsg.Show();
        $('#msgContent').html(s);
    }
    $('#btnUpload').click(function () {
        divUploading.Show();
        $.ajaxFileUpload({
            url: '../Common/Upload.aspx',
            secureuri: false,
            dataType: 'json',
            fileElementId: 'txtFile',
            success: function (data) {
                divUploading.Close();
                if (data.fileType == 'unknow') {
                    tips('文件格式不正确，目前支持的文件格式包括 .mp4 .jpg .png，且大小不能超过40MB');
                    return;
                }
                if (data.fileType == 'pic') {
                    //按格式显示
                    fileType = '1';
                    var fullName = filePath + data.fileName;
                    $('#imgCurrent').attr('src', fullName);
                    $('[id$="hdfImg"]').val(data.fileName);
                    tips('上传完成');
                    var t = setTimeout(function () {
                        divMsg.Close();
                        clearTimeout(t);
                    }, 1000)
                }
                if (data.error == 'wrong type') {
                    tips('请先选择文件，并确保格式正确！');
                }
            }
        })
    })
    $('#hideEdit').click(function () {
        divEdit.Close();
    })
    $('#hideMsg').click(function () {
        divMsg.Close();
    })
    $('#btnAdd').click(function () {
        divEdit.Show();
        $('[id$="hdfProductionID"]').val("");
        $('[id$="hdfImg"]').val("");
    })
    $('.btnEdit').each(function (index, elt) {//编辑
        $(elt).click(function () {
            divEdit.Show();
            var args = $(elt).attr('args');
            //加载
            $.get('../Common/AjaxHandler.aspx', {
                pid: args,
                handler: 'getProduction',
                r: Math.random()
            }, function (data) {
                var o = JSON.parse(data);
                var name = o[0]["Desc"];
                var url = o[0]["imgUrl"];
                var rate = o[0]["rate"];
                $('[id$="hdfProductionID"]').val(o[0]["ProductionID"]);
                $('#txtProName').val(name);
                $('#imgCurrent').attr("src", filePath + url);
                $('[id$="hdfImg"]').val(url);
                $('#txtRate').val(rate);
            })
        })
    })
    $('#btnSave').click(function () {
        var proid = $('[id$="hdfProductionID"]').val();
        if (proid == "") {//新增 
            var name = $('#txtProName').val();
            var url = $('#imgCurrent').attr("src");
            var rate = $('#txtRate').val();
            var url = $("[id$='hdfImg']").val();
            if (name == "") {
                tips("请输入产品名称！");
                return;
            }
            if (rate == "") {
                tips("请输入产品系数！");
                return;
            }
            $.post('../Common/AjaxHandler.aspx', {
                handler: 'addProduction',
                name: name,
                url: url,
                rate: rate
            }, function (data) {
                if (data == "OK") {
                    tips("新增成功");
                    var t = setTimeout(function () {
                        divMsg.Close();
                        divEdit.Close();
                        $('#txtProName').val("");
                        $('#imgCurrent').attr("src", "");
                        $('#txtRate').val("");
                        location.href = location.href;
                    }, 1000);
                }
                else {
                    tips("新增失败！");
                }
            })
        }
        else {//编辑 
            var name = $('#txtProName').val();
            var url = $('#imgCurrent').attr("src");
            var rate = $('#txtRate').val();
            var url = $("[id$='hdfImg']").val();
            if (name == "") {
                tips("请输入产品名称！");
                return;
            }
            if (rate == "") {
                tips("请输入产品系数！");
                return;
            }
            $.post('../Common/AjaxHandler.aspx', {
                handler: 'editProduction',
                name: name,
                url: url,
                rate: rate,
                proid: proid
            }, function (data) {
                if (data == "OK") {
                    tips("修改成功！");
                    var t = setTimeout(function () {
                        divMsg.Close();
                        divEdit.Close();
                        $('#txtProName').val("");
                        $('#imgCurrent').attr("src", "");
                        $('#txtRate').val("");
                        location.href = location.href;
                    }, 1000);
                }
                else {
                    tips("修改失败！");
                }
            })
        }
    })
} ())
